package com.zhangdi.zddemo.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2024.5.9
 */
public class ScheduledThreadPoolTest {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);

        pool.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("1111111111111111111");
            }
        }, 3, 3, TimeUnit.SECONDS);

        try {
            Thread.sleep(7000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ScheduledFuture future2 = pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("22222222222222222222");
            }
        }, 1, 2, TimeUnit.SECONDS);



        pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("333333333333333333333");

            }
        }, 0, 1, TimeUnit.SECONDS);

        try {
            TimeUnit.SECONDS.sleep(15);
            future2.cancel(false);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
